#include "ringBuffer.h"

// int ringBuffer_init(struct ringBufferStu *ringBuffer, uint8_t *buffer, const uint8_t size){
//     if (ringBuffer == NULL || buffer == NULL || size == 0) {
//         return -1; // 错误：无效参数
//     }
    
//     ringBuffer->buffer = buffer;
//     ringBuffer->size = size;
//     ringBuffer->head = 0;
//     ringBuffer->tail = 0;
//     ringBuffer->count = 0;

//     return 0; // 成功
// }

int ringBuffer_push(struct ringBufferStu *ringBuffer, uint8_t data){
    if (ringBuffer == NULL || ringBuffer->buffer == NULL || ringBuffer->count >= ringBuffer->size) {
        return -1; // 错误：缓冲区已满或无效
    }
    
    ringBuffer->buffer[ringBuffer->head] = data;
    ringBuffer->head = (ringBuffer->head + 1) % ringBuffer->size;
    ringBuffer->count++;

    return 0; // 成功
}

int ringBuffer_pop(struct ringBufferStu *ringBuffer, uint8_t *data){
    if (ringBuffer == NULL || ringBuffer->buffer == NULL || ringBuffer->count == 0) {
        return -1; // 错误：缓冲区为空或无效
    }
    
    *data = ringBuffer->buffer[ringBuffer->tail];
    ringBuffer->tail = (ringBuffer->tail + 1) % ringBuffer->size;
    ringBuffer->count--;

    return 0; // 成功
}

int ringBuffer_peek_head(struct ringBufferStu *ringBuffer, uint8_t *data){
    if (ringBuffer == NULL || ringBuffer->buffer == NULL || ringBuffer->count == 0) {
        return -1; // 错误：缓冲区为空或无效
    }
    
    *data = ringBuffer->buffer[ringBuffer->tail];
    return 0; // 成功
}

int ringBuffer_peek_tail(struct ringBufferStu *ringBuffer, uint8_t *data){
    if (ringBuffer == NULL || ringBuffer->buffer == NULL || ringBuffer->count == 0) {
        return -1; // 错误：缓冲区为空或无效
    }
    
    *data = ringBuffer->buffer[(ringBuffer->head - 1 + ringBuffer->size) % ringBuffer->size];
    return 0; // 成功
}

int ringBuffer_is_empty(struct ringBufferStu *ringBuffer){
    if (ringBuffer == NULL) {
        return -1; // 错误：无效参数
    }
    
    return (ringBuffer->count == 0);
}

int ringBuffer_is_full(struct ringBufferStu *ringBuffer){
    if (ringBuffer == NULL) {
        return -1; // 错误：无效参数
    }
    
    return (ringBuffer->count == ringBuffer->size);
}

int ringBuffer_clear(struct ringBufferStu *ringBuffer){
    if (ringBuffer == NULL) {
        return -1; // 错误：无效参数
    }
    
    ringBuffer->head = 0;
    ringBuffer->tail = 0;
    ringBuffer->count = 0;

    return 0; // 成功
}

int ringBuffer_get_count(struct ringBufferStu *ringBuffer){
    if (ringBuffer == NULL) {
        return -1; // 错误：无效参数
    }
    
    return ringBuffer->count;
}


